﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenLib
{
    public class Controller
    {
        public Controller()
        {
            this.inputInclude = new List<bool>();
            this.param = new GenParam();
            this.logic = new GenCodeLogic();
            this.creater = new ParamCreater();

            for (int i = 0; i < MAX_BASE_COUNT; ++i)
            {
                this.param.AddBases("");
                this.param.AddInclude("");
                this.inputInclude.Add(false);
            }

            this.cppCodes = new Codes();
            this.hCodes = new Codes();
        }

        public void UpdateClass(String className)
        {
            this.param.Class = className;
            this.creater.CreateFilename(this.param);
            if (this.param.Namespace.Length != 0)
            {
                this.param.HeaderGuard = this.creater.CreateHeaderGuard(this.param.Class, this.param.Namespace);
            }

            this.NotifyListener();
        }

        public void UpdateNamespace(String ns)
        {
            this.param.Namespace = ns;
            if (this.param.Class.Length != 0)
            {
                this.param.HeaderGuard = this.creater.CreateHeaderGuard(this.param.Class, this.param.Namespace);
            }

            this.NotifyListener();
        }

        public void UpdateBase(int index, String baseName)
        {
            if (index >= MAX_BASE_COUNT)
            {
                return;
            }

            this.param.ModifyBases(index, baseName);

            if (!this.inputInclude[index])
            {
                String include = this.creater.CreateInclude(baseName);
                this.param.ModifyInclude(index, include);
                this.collectView.UpdateInclude(index, this.param.Includes[index]);
            }

            this.NotifyListener();
        }

        public void UpdateInclude(int index, String include)
        {
            if (index >= MAX_BASE_COUNT)
            {
                return;
            }

            this.param.ModifyInclude(index, include);
            this.inputInclude[index] = true;

            this.NotifyListener();
            this.collectView.UpdateInclude(index, this.param.Includes[index]);
        }

        private void NotifyListener()
        {
            this.logic.GenCppFile(this.param, cppCodes);
            this.logic.GenHFile(this.param, hCodes);

            this.hView.UpdateCodes(this.hCodes);
            this.cppView.UpdateCodes(this.cppCodes);
        }

        public GenParam Param
        {
            get { return this.param; }
        }

        public Codes HCodes
        {
            get { return this.hCodes; }
        }

        public Codes CppCodes
        {
            get { return this.cppCodes; }
        }

        public View CppView
        {
            set { this.cppView = value; }
        }

        public View HView
        {
            set { this.hView = value; }
        }

        public View CollectView
        {
            set { this.collectView = value; }
        }

        private GenParam param;
        private GenCodeLogic logic;
        private ParamCreater creater;
        private List<bool> inputInclude;

        private View hView;
        private View cppView;
        private View collectView;

        private Codes cppCodes;
        private Codes hCodes;

        private const int MAX_BASE_COUNT = 3;
    }
}
